﻿#pragma execution_character_set("utf-8")

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>

#define sys_out qDebug()

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QTimer::singleShot(10*60*1000,[this]{
        close();
    });

    setWindowTitle("百度人脸识别综合应用V2.0  香菇滑稽 blog.csdn.net/wwplh5520370  鹅厂通用号：784765727");

    ButtonG = new QButtonGroup(this);
    ButtonG->addButton(ui->radioButton1,1);
    ButtonG->addButton(ui->radioButton2,2);
    ui->radioButton1->setChecked(true);

    swidget = new ScreenWidget;
    connect(swidget,&ScreenWidget::overShot,this,&MainWindow::drawPixmap);

    BaiduFace = new BaiduFaceOnline(this);
    connect(BaiduFace, &BaiduFaceOnline::FaceverifyResult, this,&MainWindow::getFaceverifyResult);
    connect(BaiduFace, &BaiduFaceOnline::DetectResult, this,&MainWindow::getDetectResult);
    connect(BaiduFace, &BaiduFaceOnline::MatchResult, this,&MainWindow::getMatchResult);
    connect(BaiduFace, &BaiduFaceOnline::SerchResult, this,&MainWindow::getSerchResult);
    connect(BaiduFace, &BaiduFaceOnline::RegisterResult, this,&MainWindow::getRegisterResult);

    connect(&cameraview, &CameraView::fcaptrued, this, &MainWindow::readCapturedImage);
    connect(&cameraview, &CameraView::icaptrued, this, &MainWindow::reciveCapturedImage);

    QString file_path = QApplication::applicationDirPath() + "/userParameters/config.ini";
    if(!QFile::exists(file_path))
    {
        QMessageBox::information(this, "提示", "未找到文件" + file_path + ".\n请先设置用户参数.");
    }
    else
    {
        QSettings iniFile(file_path, QSettings::IniFormat);
        iniFile.beginGroup("API-parameters");
        BaiduFace->getAccToken(iniFile.value("API_key").toString(), iniFile.value("Secret_key").toString());
        iniFile.endGroup();
    }
}

MainWindow::~MainWindow()
{
    swidget->deleteLater();
    delete ui;
}

void MainWindow::closeEvent(QCloseEvent* event)
{
    cameraview.close();
}

void MainWindow::readCapturedImage(int id, const QString& fileName)
{
    Q_UNUSED(id);
    ui->image_path->setText(fileName);
    switch (ui->tabWidget->currentIndex())
    {
    case 0:
    {
        ui->image_path->setText(fileName);
        break;
    }
    case 1:
    {
        ui->image_path_2->setText(fileName);
        break;
    }
    case 2:
    {
        ui->image_path_3->setText(fileName);
        break;
    }
    case 3:
    {
        if(ButtonG->checkedId() == 1)
        {
            ui->match1_path->setText(fileName);
        }
        else
        {
            ui->match2_path->setText(fileName);
        }
        break;
    }
    }
}


void MainWindow::reciveCapturedImage(int id, const QImage& preview)
{
    Q_UNUSED(id);
    Image = preview;
    switch (ui->tabWidget->currentIndex())
    {
    case 0:
    {
        ui->image_view->setPixmap(QPixmap::fromImage(Image.scaled(ui->image_view->size(),Qt::KeepAspectRatio)));
        break;
    }
    case 1:
    {
        ui->image_view_2->setPixmap(QPixmap::fromImage(Image.scaled(ui->image_view_2->size(),Qt::KeepAspectRatio)));
        break;
    }
    case 2:
    {
        ui->image_view_3->setPixmap(QPixmap::fromImage(Image.scaled(ui->image_view_3->size(),Qt::KeepAspectRatio)));
        break;
    }
    case 3:
    {
        if(ButtonG->checkedId() == 1)
        {
            ui->image_view_41->setPixmap(QPixmap::fromImage(Image.scaled(ui->image_view_41->size(),Qt::KeepAspectRatio)));
        }
        else
        {
            ui->image_view_42->setPixmap(QPixmap::fromImage(Image.scaled(ui->image_view_42->size(),Qt::KeepAspectRatio)));
        }
        break;
    }
    }
}

void MainWindow::setIniFile(const QString& api_key, const QString& secret_key)
{
    if(api_key.isEmpty() | secret_key.isEmpty())
    {
        QMessageBox::information(this, "提示", "参数设置错误.");
        return;
    }
    QString fileName = QApplication::applicationDirPath() + "/userParameters/config.ini";
    QSettings iniFile(fileName, QSettings::IniFormat);
    iniFile.beginGroup("API-parameters");
    iniFile.setValue("API_key", api_key);
    iniFile.setValue("Secret_key", secret_key);
    iniFile.endGroup();
    BaiduFace->getAccToken(api_key, secret_key);
}

void MainWindow::on_open_img_triggered()
{
    QString filename = QFileDialog::getOpenFileName();
    if(!filename.isEmpty())
    {
        Image.load(filename);
        switch (ui->tabWidget->currentIndex())
        {
        case 0:
        {
            ui->image_path->setText(filename);
            ui->image_view->setPixmap(QPixmap::fromImage(Image.scaled(ui->image_view->size(),Qt::KeepAspectRatio)));
            break;
        }
        case 1:
        {
            ui->image_path_2->setText(filename);
            ui->image_view_2->setPixmap(QPixmap::fromImage(Image.scaled(ui->image_view_2->size(),Qt::KeepAspectRatio)));
            break;
        }
        case 2:
        {
            ui->image_path_3->setText(filename);
            ui->image_view_3->setPixmap(QPixmap::fromImage(Image.scaled(ui->image_view_3->size(),Qt::KeepAspectRatio)));
            break;
        }
        case 3:
        {
            if(ButtonG->checkedId() == 1)
            {
                ui->match1_path->setText(filename);
                ui->image_view_41->setPixmap(QPixmap::fromImage(Image.scaled(ui->image_view_41->size(),Qt::KeepAspectRatio)));
            }
            else
            {
                ui->match2_path->setText(filename);
                ui->image_view_42->setPixmap(QPixmap::fromImage(Image.scaled(ui->image_view_42->size(),Qt::KeepAspectRatio)));
            }
            break;
        }
        }
    }
}

void MainWindow::on_act_baidu_triggered()
{
    switch (ui->tabWidget->currentIndex())
    {
        case 0:
        {
            BaiduFace->Faceverify(QImage(ui->image_path->text()));
            break;
        }
        case 1:
        {
            BaiduFace->FaceDetect(QImage(ui->image_path_2->text()));
            break;
        }
        case 2:
        {
            BaiduFace->FaceSerch(QImage(ui->image_path_3->text()));
            break;
        }
        case 3:
        {
            BaiduFace->FaceMatch(QImage(ui->match1_path->text()), QImage(ui->match2_path->text()));
            break;
        }
    }
}

void MainWindow::drawPixmap(const QPixmap& pixmap)
{
    QString filename = QString("%1/screen_shot.png").arg(qApp->applicationDirPath());
    setWindowState(Qt::WindowNoState);
    if(!filename.isEmpty())
    {
        Image.load(filename);
        switch (ui->tabWidget->currentIndex())
        {
        case 0:
        {
            ui->image_path->setText(filename);
            ui->image_view->setPixmap(QPixmap::fromImage(pixmap.toImage().scaled(ui->image_view->size(), Qt::KeepAspectRatio)));
            break;
        }
        case 1:
        {
            ui->image_path_2->setText(filename);
            ui->image_view_2->setPixmap(QPixmap::fromImage(pixmap.toImage().scaled(ui->image_view_2->size(), Qt::KeepAspectRatio)));
            break;
        }
        case 2:
        {
            ui->image_path_3->setText(filename);
            ui->image_view_3->setPixmap(QPixmap::fromImage(pixmap.toImage().scaled(ui->image_view_3->size(), Qt::KeepAspectRatio)));
            break;
        }
        case 3:
        {
            if(ButtonG->checkedId() == 1)
            {
                ui->match1_path->setText(filename);
                ui->image_view_41->setPixmap(QPixmap::fromImage(pixmap.toImage().scaled(ui->image_view_41->size(), Qt::KeepAspectRatio)));
            }
            else
            {
                ui->match2_path->setText(filename);
                ui->image_view_42->setPixmap(QPixmap::fromImage(pixmap.toImage().scaled(ui->image_view_42->size(), Qt::KeepAspectRatio)));
            }
            break;
        }
        }
    }
}
void MainWindow::on_shot_screen_triggered()
{
    setWindowState(Qt::WindowMinimized);
    //单次定时器触发截图，等待界面最小化完成
    QTimer::singleShot(300, swidget, &ScreenWidget::showFullScreen);
}

void MainWindow::on_help_about_triggered()
{
    QMessageBox::information(this,"帮助&关于","打开图片快捷键：Ctrl + O\n面向百度快捷键：Ctrl + U\n截图快捷键：Alt + S\n作者：香菇滑稽之谈\n主页：blog.csdn.net/wwplh5520370");
}

void MainWindow::on_actSetPara_triggered()
{
    Parameters ParaDialog;
    ParaDialog.exec();
    if(ParaDialog.result() == QDialog::Accepted)
    {
        setIniFile(ParaDialog.API_key(), ParaDialog.Secret_key());
    }
}

void MainWindow::getFaceverifyResult(const QString& result)
{
    ui->Result->append(result);
//    ui->Result->append("----------------------");
}
void MainWindow::getDetectResult(const QString&  content)
{
    ui->Result_2->append(content);
//    ui->Result_2->append("----------------------");
}
void MainWindow::getSerchResult(const QString&  content)
{
    ui->Result_3->append(content);
//    ui->Result_3->append("----------------------");
}
void MainWindow::getRegisterResult(const QString&  content)
{
    ui->Result_3->append(content);
//    ui->Result_3->append("----------------------");
}
void MainWindow::getMatchResult(const QString&  content)
{
    ui->Result_4->setText(content);
}

void MainWindow::on_face_register_clicked()
{
    QString group_name = ui->group_id->text();
    QString user_name = ui->user_id->text();
    if(!group_name.isEmpty() && !user_name.isEmpty())
    {
        BaiduFace->FaceRegister(QImage(ui->image_path_3->text()), group_name, user_name);
        return;
    }
    QMessageBox::information(this,"提示","注册信息不完整！");
}

void MainWindow::on_ActGrabImage_triggered()
{
    cameraview.captrueImage();
}

void MainWindow::on_ActCameraView_triggered()
{
    cameraview.show();
    cameraview.openCamera();
}

void MainWindow::on_ActCloseCamera_triggered()
{
    cameraview.closeCamera();
    cameraview.close();
}

